<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Provider;

use App\Entity\Room;
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

class RoomManager
{
    protected LoggerInterface $logger;

    protected array $rooms;

    public function __construct(LoggerFactory $loggerFactory)
    {
        $this->logger = $loggerFactory->get('room_manager');
    }

    public function hasRoom(int $roomId): bool
    {
        return isset($this->rooms[$roomId]);
    }

    public function getRoom(int $roomId): Room
    {
        if (! $this->hasRoom($roomId)) {
            throw new \Exception('room not found: ' . $roomId);
        }
        return $this->rooms[$roomId];
    }

    public function addRoom(Room $room)
    {
        $this->rooms[$room->getRoomId()] = $room;
    }

    public function fetchRooms(): array
    {
        return $this->rooms;
    }
}
